package com.vichat.api.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.vichat.api.vo.RequestBusiRecord;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Date;

public class VichatHttpPost {
    private static Log logger = LogFactory.getLog(VichatHttpPost.class);
    private static String defaultUrl = "http://47.104.186.84/api/post";
    private static String APP_TOKEN = "78461154";
    private static RequestBusiRecord requestBusiRecord = null;

    public static RequestBusiRecord getInstance() {
        if (requestBusiRecord == null) {
            requestBusiRecord = new RequestBusiRecord();
            requestBusiRecord.setAppId("10000");
            requestBusiRecord.setAppType("S");

            String timestamp = String.valueOf(new Date().getTime());
            requestBusiRecord.setTimestamp(timestamp);

            String sign = Md5Util.Md5Encode(APP_TOKEN + timestamp, null);
            requestBusiRecord.setSign(sign);
            return requestBusiRecord;
        }
        return requestBusiRecord;
    }

    public static String registerCompany(String json) {
        String url = defaultUrl;

        JSONObject contentJson = new JSONObject();
        RequestBusiRecord requestBusiRecord = getInstance();

        contentJson.put("company", json);

        requestBusiRecord.setRequestContent(contentJson.toJSONString());
        requestBusiRecord.setMethod("vc.register_company");

        String respJson = JSON.toJSONString(requestBusiRecord);
        return post(url, respJson);
    }

    /**
     * 添加用户
     *
     * @param json
     * @return
     */
    public static String addUser(String json) {
        String url = defaultUrl;

        JSONObject contentJson = new JSONObject();
        RequestBusiRecord requestBusiRecord = getInstance();

        contentJson.put("user", json);

        requestBusiRecord.setRequestContent(contentJson.toJSONString());
        requestBusiRecord.setMethod("vc.add_user");

        String respJson = JSON.toJSONString(requestBusiRecord);
        return post(url, respJson);
    }

    /**
     * 修改用户信息
     *
     * @param json
     * @return
     */
    public static String modeUser(String json) {
        String url = defaultUrl;

        JSONObject contentJson = new JSONObject();
        RequestBusiRecord requestBusiRecord = getInstance();

        contentJson.put("user", json);

        requestBusiRecord.setRequestContent(contentJson.toJSONString());
        requestBusiRecord.setMethod("vc.mod_user");

        String respJson = JSON.toJSONString(requestBusiRecord);
        return post(url, respJson);
    }

    /**
     * 删除用户
     *
     * @param json
     * @return
     */
    public static String delUser(String json) {
        String url = defaultUrl;

        JSONObject contentJson = new JSONObject();
        RequestBusiRecord requestBusiRecord = getInstance();

        contentJson.put("user", json);

        requestBusiRecord.setRequestContent(contentJson.toJSONString());
        requestBusiRecord.setMethod("vc.del_user");

        String respJson = JSON.toJSONString(requestBusiRecord);
        return post(url, respJson);
    }

    /**
     * 重置密码
     *
     * @param json
     * @return
     */
    public static String resetPasswd(String json) {
        String url = defaultUrl;

        JSONObject contentJson = new JSONObject();
        RequestBusiRecord requestBusiRecord = getInstance();

        contentJson.put("user", json);

        requestBusiRecord.setRequestContent(contentJson.toJSONString());
        requestBusiRecord.setMethod("vc.reset_passwd");

        String respJson = JSON.toJSONString(requestBusiRecord);
        return post(url, respJson);
    }

    /**
     * 用户激活(延期)
     *
     * @param json
     * @return
     */
    public static String acitivityUser(String json) {
        String url = defaultUrl;

        JSONObject contentJson = new JSONObject();
        RequestBusiRecord requestBusiRecord = getInstance();

        contentJson.put("user", json);

        requestBusiRecord.setRequestContent(contentJson.toJSONString());
        requestBusiRecord.setMethod("vc.activity_user");

        String respJson = JSON.toJSONString(requestBusiRecord);
        return post(url, respJson);
    }

    /**
     * 添加频道
     *
     * @param json
     * @return
     */
    public static String addRoom(String json) {
        String url = defaultUrl;

        JSONObject contentJson = new JSONObject();
        RequestBusiRecord requestBusiRecord = getInstance();

        contentJson.put("room", json);

        requestBusiRecord.setRequestContent(contentJson.toJSONString());
        requestBusiRecord.setMethod("vc.add_room");

        String respJson = JSON.toJSONString(requestBusiRecord);
        return post(url, respJson);
    }

    /**
     * 修改频道信息
     *
     * @param json
     * @return
     */
    public static String modRoom(String json) {
        String url = defaultUrl;

        JSONObject contentJson = new JSONObject();
        RequestBusiRecord requestBusiRecord = getInstance();

        contentJson.put("room", json);

        requestBusiRecord.setRequestContent(contentJson.toJSONString());
        requestBusiRecord.setMethod("vc.mod_room");

        String respJson = JSON.toJSONString(requestBusiRecord);
        return post(url, respJson);
    }

    /**
     * 删除频道
     *
     * @param json
     * @return
     */
    public static String delRoom(String json) {
        String url = defaultUrl;

        JSONObject contentJson = new JSONObject();
        RequestBusiRecord requestBusiRecord = getInstance();

        contentJson.put("room", json);

        requestBusiRecord.setRequestContent(contentJson.toJSONString());
        requestBusiRecord.setMethod("vc.del_room");

        String respJson = JSON.toJSONString(requestBusiRecord);
        return post(url, respJson);
    }

    /**
     * 添加频道用户
     *
     * @param json
     * @return
     */
    public static String addRoomUser(String json) {
        String url = defaultUrl;

        JSONObject contentJson = new JSONObject();
        RequestBusiRecord requestBusiRecord = getInstance();

        contentJson.put("roomUser", json);

        requestBusiRecord.setRequestContent(contentJson.toJSONString());
        requestBusiRecord.setMethod("vc.add_roomuser");

        String respJson = JSON.toJSONString(requestBusiRecord);
        return post(url, respJson);
    }

    /**
     * 删除频道用户
     *
     * @param json
     * @return
     */
    public static String delRoomUser(String json) {
        String url = defaultUrl;

        JSONObject contentJson = new JSONObject();
        RequestBusiRecord requestBusiRecord = getInstance();

        contentJson.put("roomUser", json);

        requestBusiRecord.setRequestContent(contentJson.toJSONString());
        requestBusiRecord.setMethod("vc.del_roomuser");

        String respJson = JSON.toJSONString(requestBusiRecord);
        return post(url, respJson);
    }

    /**
     * 获取用户轨迹信息
     *
     * @param json
     * @return
     */
    public static String getLocationLog(String json) {
        String url = defaultUrl;

        JSONObject contentJson = new JSONObject();
        RequestBusiRecord requestBusiRecord = getInstance();

        contentJson.put("locationData", json);

        requestBusiRecord.setRequestContent(contentJson.toJSONString());
        requestBusiRecord.setMethod("vc.get_locationlog");

        String respJson = JSON.toJSONString(requestBusiRecord);
        return post(url, respJson);
    }

    /**
     * 获取企业在线用户定位信息
     *
     * @param json
     * @return
     */
    public static String getOnlineLocation(String json) {
        String url = defaultUrl;

        JSONObject contentJson = new JSONObject();
        RequestBusiRecord requestBusiRecord = getInstance();

        contentJson.put("company", json);

        requestBusiRecord.setRequestContent(contentJson.toJSONString());
        requestBusiRecord.setMethod("vc.get_onlinelocation");

        String respJson = JSON.toJSONString(requestBusiRecord);
        return post(url, respJson);
    }

    /**
     * 获取指定用户定位信息
     *
     * @param json
     * @return
     */
    public static String getSelectLocation(String json) {
        String url = defaultUrl;

        JSONObject contentJson = new JSONObject();
        RequestBusiRecord requestBusiRecord = getInstance();

        contentJson.put("user", json);

        requestBusiRecord.setRequestContent(contentJson.toJSONString());
        requestBusiRecord.setMethod("vc.get_selectuserlocation");

        String respJson = JSON.toJSONString(requestBusiRecord);
        return post(url, respJson);
    }

    /**
     * 设置用户定位开关
     *
     * @param json
     * @return
     */
    public static String locationSwitch(String json) {
        String url = defaultUrl;

        JSONObject contentJson = new JSONObject();
        RequestBusiRecord requestBusiRecord = getInstance();

        contentJson.put("user", json);

        requestBusiRecord.setRequestContent(contentJson.toJSONString());
        requestBusiRecord.setMethod("vc.location_switch");

        String respJson = JSON.toJSONString(requestBusiRecord);
        return post(url, respJson);
    }

    /**
     * 创建临时频道
     *
     * @param json
     * @return
     */
    public static String createPersonalChannel(String json) {
        String url = defaultUrl;

        JSONObject contentJson = new JSONObject();
        RequestBusiRecord requestBusiRecord = getInstance();

        contentJson.put("room", json);

        requestBusiRecord.setRequestContent(contentJson.toJSONString());
        requestBusiRecord.setMethod("vc.create_personal_room");

        String respJson = JSON.toJSONString(requestBusiRecord);
        return post(url, respJson);
    }

    /**
     * 添加企业
     *
     * @param json
     * @return
     */
    public static String addOrg(String json) {
        String url = defaultUrl;

        JSONObject contentJson = new JSONObject();
        RequestBusiRecord requestBusiRecord = getInstance();

        contentJson.put("org", json);

        requestBusiRecord.setRequestContent(contentJson.toJSONString());
        requestBusiRecord.setMethod("vc.add_org");

        String respJson = JSON.toJSONString(requestBusiRecord);
        return post(url, respJson);
    }

    /**
     * 修改企业信息
     *
     * @param json
     * @return
     */
    public static String modOrg(String json) {
        String url = defaultUrl;

        JSONObject contentJson = new JSONObject();
        RequestBusiRecord requestBusiRecord = getInstance();

        contentJson.put("org", json);

        requestBusiRecord.setRequestContent(contentJson.toJSONString());
        requestBusiRecord.setMethod("vc.mod_org");

        String respJson = JSON.toJSONString(requestBusiRecord);
        return post(url, respJson);
    }

    /**
     * 删除企业
     *
     * @param json
     * @return
     */
    public static String delOrg(String json) {
        String url = defaultUrl;

        JSONObject contentJson = new JSONObject();
        RequestBusiRecord requestBusiRecord = getInstance();

        contentJson.put("org", json);

        requestBusiRecord.setRequestContent(contentJson.toJSONString());
        requestBusiRecord.setMethod("vc.del_org");

        String respJson = JSON.toJSONString(requestBusiRecord);
        return post(url, respJson);
    }

    /**
     * post 方法
     *
     * @param url
     * @param json
     * @return
     */
    private static String post(String url, String json) {
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost method = new HttpPost(url);
        String responseBody = null;
        try {
            // 建立一个NameValuePair数组，用于存储欲传送的参数
            method.addHeader("Content-type", "application/json; charset=utf-8");
            method.setHeader("Accept", "application/json");
            method.setEntity(new StringEntity(json, Charset.forName("UTF-8")));
            long startTime = System.currentTimeMillis();
            HttpResponse response = httpClient.execute(method);
            long endTime = System.currentTimeMillis();
            int statusCode = response.getStatusLine().getStatusCode();
            System.out.println("statusCode:" + statusCode);
            System.out.println("调用API 花费时间(单位：毫秒)：" + (endTime - startTime));
            if (statusCode != HttpStatus.SC_OK) {
                logger.error("Method failed:" + response.getStatusLine());
            }
            // Read the response body
            responseBody = EntityUtils.toString(response.getEntity());
            System.out.println("输出参数：" + responseBody);
        } catch (IOException e) {
            // 网络错误
            e.printStackTrace();
        } finally {
            return responseBody;
        }
    }

    public static void main(String[] args) {
        VichatHttpPost.addUser("");
    }
}
